package com.hchun.dao;

import com.hchun.io.Resources;
import com.hchun.pojo.User;
import com.hchun.sqlSession.SqlSession;
import com.hchun.sqlSession.SqlSessionFactory;
import com.hchun.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    public List<User> findAll() throws Exception {
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        // 创建sqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
//        User user = new User(1,"hchun");
//        User user2 = sqlSession.selectOne("user.selectOne",user);
//        System.out.println(sqlSession.selectList("user.selectList"));
        return sqlSession.selectList("user.selectList");
    }

    public User findByCondition(User user) throws Exception {
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        // 创建sqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
//        User user = new User(1,"hchun");
//        User user2 = sqlSession.selectOne("user.selectOne",user);
        System.out.println(sqlSession.selectOne("user.selectList", user));
        return null;
    }
}
